import random
import os
import re
import time


#f = open('ftest.txt','w+')
#f.write('*****jski信息发布助手*****'+'\n'+'1:操作指令\n'+'****jski西南公告更新****'+'\n'+'单板教学、免费滑行视频分析@六岁@天一'+'\n')
#f.close()


#file = open("ftest.txt",'r')
#lines = file.readlines()
#num = int((lines[-3])[0:1])


def clearmsg():
    f = open('ftest.txt','w+')
    f.write('*****信息发布助手*****'+'\n'+'1:指令 j发+内容(j发明天融创) j删序号(j删5) j(显示)\n')
    f.close()
def addmsg(s,name):
    file = open("ftest.txt",'r')
    lines = file.readlines()
    timenow = time.strftime('%Y-%m-%d',time.localtime(time.time()))
    num = int((lines[-1])[0:1])
    num = num+2
    #delmsg('****jski西南公告更新****')
    #delmsg('单板教学、免费滑行视频分析@六岁 @天一')
    f = open('ftest.txt','a')
    f.write(str(num)+':(发布时间'+str(timenow)+')'+s+'@%s '%(name)+'\n')
    #f.write('****jski西南公告更新****\n')
    #f.write('单板教学、免费滑行视频分析@六岁 @天一\n')
    f.close()
def showmsg():
    with open('ftest.txt', 'r') as f:
    #print(f.read())
        txtmsg = str(f.read())
    f.close()
    return txtmsg

def delmsg(s):
    lines = (i for i in open('ftest.txt','r') if s not in i )
    f = open('ftest_new.txt', 'w', encoding="utf-8")
    f.writelines(lines)
    f.close()
    os.rename('ftest.txt', 'ftest.bak')
    os.rename('ftest_new.txt', 'ftest.txt')
    os.remove('ftest.bak')
    f = open('ftest.txt', 'r', encoding="utf-8")
    f.close()

def addnum():
    file = open("ftest.txt",'r')
    lines = file.readlines()
    n = 0
    with open("ftest.txt",'w') as f:
        for line in lines:
            if lines[1] == lines[-1]:
                pass
            elif n in [0]:
                f.write(line)
                n=n+1
            else:
                newline = line[2:]
                a=n*2-1
                f.write(str(a)+':'+newline)
                n=n+1
    f.close()

def delnum(delnum):
    file = open("ftest.txt",'r')
    lines = file.readlines()
    n = 0
    newdelnum = (delnum+1)/2
    if lines[1] == lines[-2]:
        clearmsg()
    else:
        with open("ftest.txt",'w') as f:
            for line in lines:
                if n in [0]:
                    f.write(line)
                    n=n+1
                elif n == newdelnum:
                    n = n + 1
                else:
                    newline = line[2:]
                    a = n*2-1
                    f.write(str(n)+':'+newline) 
                    n=n+1
            f.close()

def msghelp(act,name):
    file = open("ftest.txt",'r')
    lines = file.readlines()
    num = int((lines[-1])[0:1])
    act = act.replace('\n', '')
    act = act.replace(' ','')
    if 'j发' in act:
        act = act.replace('j发','')
        addmsg(act,name)
        #print(showmsg())
    elif 'j删' in act:
        act = act.replace('j删','')
        act=int(act)
        if int(act)==1:
            print('不可删除')
        elif num == act ==3:
            clearmsg()
        elif act>num:
            print('没有这一行')
        else:
            delnum(act)
            addnum()
            #print(showmsg())
    elif 'j显示' in act:
        pass
        #print(showmsg())
    elif 'j清空' in act:
        clearmsg()
    msgshow = showmsg()
    return msgshow
